﻿/*
 *
 * @author= MARIANO
 * @version= 1.0
 * 
 */
 
 import com.dinmax.flash.sof.*;
 import com.dinmax.flash.utilities.classes.ClassFinder;
 import com.dinmax.flash.utilities.communication.XMLCommunication;
 import com.dinmax.flash.utilities.communication.XMLCommunicationListener;
 
 class SOFXMLManager extends SOFManager implements XMLCommunicationListener {
	
	private var data:SOFServiceData;
	private var service:SOFService;
	private var XMLCom:XMLCommunication;
	
	function SOFXMLManager(){
		//trace("new SOFXMLManager.as");
	}
	
	public function execute(service:SOFService):Void {
		//trace("SOFXMLManager.as | execute()");
		this.service = service;
		data = service.getParameters();
		XMLCom = XMLCommunication.newInstance(SOFManager.GATEWAY, this);
		XMLCom.send(this.createXML(data));
	}
	
	public function error(xml:XML):Void {
		
	};
	
	public function finished(xml:XML):Void{
		trace("\n----\nSOFXMLManager.as | finished | xml= "+xml);
		this.service.result(SOFServiceDataFactory.getSOFServiceData(xml));	
	};
	
	private function createXML(data:SOFServiceData):XML {
		var sXML:String = new String("");
		
		sXML += '<service name="'+service.getName()+'" >';
		sXML += '<classes>';
		for(var i:Number=0; i < data.getClasses().length; i++){
			sXML +='<class name="'+data.getClasses()[i].getKey()+'" >';
			var attributes:Array = ClassFinder.getAttributes(data.getClasses()[i].getValue()).getItems();
			for(var j:Number=0; j < attributes.length; j++){
				sXML += '<attribute name="'+attributes[j].getKey()+'" value="'+attributes[j].getValue()+'" />';
			}
			sXML +='</class>';
		}
		sXML += '</classes>';
		sXML += '</service>';
		
		return new XML(sXML);
	};
	
	
}; // fin de clase